package com.lei2j.state;

import java.util.Random;

/**
 * Created by lei2j on 2019/2/4.
 */
public class HasQuarterState implements State {

    private GumballaMachine gumballaMachine;

    public HasQuarterState(GumballaMachine gumballaMachine) {
        this.gumballaMachine = gumballaMachine;
    }

    @Override
    public void ejectQuarter() {
        System.out.println("退币成功");
        gumballaMachine.setState(gumballaMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("转曲柄");
        Random random = new Random(System.currentTimeMillis());
        if (random.nextInt(10)==0&&gumballaMachine.getCount()>0){
            System.out.println("恭喜您中奖了");
            gumballaMachine.setState(gumballaMachine.getWinnerState());
        }else {
            gumballaMachine.setState(gumballaMachine.getSoldState());
        }
    }
}
